﻿@model IEnumerable<CmsAdminEntityTreeNode<CmsSiteAdminMap>>
@{
    ViewBag.Title = "Modules";
    Layout = "/admin/templates/TelerikAdministration.cshtml";
}

<h1 class="g-clear">
    <i class="b-icon b-icon_h b-icon_h_products"></i>
    <span class="b-overflow">Admin Area Structure management
        <span class="b-intro-text">The following tree represents the structure of the admin area.</span>
    </span>
</h1>
@{Html.RenderPartial("ErrorPanel");}

<div id="TabStrip" class="t-widget t-tabstrip t-header">
    @Html.Partial("ConfigurationMenu", 4)
    <div style="display: block;" id="TabStrip-1" class="t-content t-state-active">
        <div class="t-content-inner g-clear">
            <div class="b-tree-view">
                @(Html.Telerik().TreeView().Name("siteMap")
                      .ShowLines(true)
                      .DragAndDrop(true)
                      .ClientEvents(events => events
                          .OnSelect("tree_onSelect")
                          .OnNodeDragging("tree_onNodeDragging")
                          .OnNodeDropped("tree_onNodeDropped"))
                      .BindTo(Model, mapping =>
                      {
                          mapping.For<CmsAdminEntityTreeNode<CmsSiteAdminMap>>(
                              binding => binding.ItemDataBound((item, node) =>
                              {
                                  item.Text = node.RootInstance.NodeTitle;
                                  item.Value = node.RootInstance.Id.ToString();
                                  item.LoadOnDemand = node.RootInstance.HasChildren;
                                  item.Expanded = node.RootInstance.ParentNodeId == null;
                              })
                                  .Children(node => node.NestedTreeNodes));
                      }))
                <div class="b-tree-view__details" id="siteMapContainer" style="display:none;"></div>
            </div>
        </div>
    </div>
</div>

@section scriptBlock{
    <script type="text/javascript">
        // <![CDATA[
        var currentViewSiteMapModel;
        function addSiteMapItem(params) {
            $.ajax(
            {
                type: "POST",
                url: '@Url.Action("add", "cmsadminsitemap")',
                data: params,
                dataType: "json",
                contentType: "application/json",
                async: false,
                success: function () {
                    window.location.href = window.location.href;
                }
            });
        }
        function treeView() {
            return $('#siteMap').data('tTreeView');
        }
        function tree_onSelect(e) {
            var nodeId = treeView().getItemValue(e.item);
            if (nodeId > 0) {
                $el = $('#siteMapContainer');
                $el.load('@Url.Action("sitemapdetails", "cmsadminsitemap")/' + nodeId, function (response, status, xhr) {
                    if (status == "error") {
                        viewModel.viewEventModel.addEvent({ eventBody: xhr.responseText });
                    }
                    else {
                        $el.fadeIn();
                        $('#siteMapSaveForm').ajaxForm(
                        {
                            success: function (response) {
                                if (response == 0) {
                                    viewModel.viewEventModel.addEvent({ eventBody: 'unknow action' });
                                } else {
                                    window.location.href = window.location.href;
                                }
                            }
                        });
                        currentViewSiteMapModel = new viewSiteMapModel(parseInt(nodeId), addSiteMapItem);
                        ko.applyBindings(currentViewSiteMapModel, $("#newNodeContainer")[0]);

                    }
                });
            }
        }
        function tree_onNodeDragging(e) {
            e.setStatusClass('drag-clue-status');
        }
        function tree_onNodeDropped(e) {

        }
        // ]]>
    </script>
}
